/**
 * Copyright 2007 American Well Systems
 * All rights reserved.
 *
 * It is illegal to use, reproduce or distribute
 * any part of this Intellectual Property without
 * prior written authorization from American Well.
 */
package org.windowkit.toolkit.databinding;

import java.awt.Component;
import java.awt.LayoutManager;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JComponent;
import javax.swing.JPanel;

/**
 * JPanel extension that provides the support for data binding.
 * All of JComponent's <code>add</code> methods have an equivalent signature defined here as <code>addBound</code>.
 * These methods take an extra String parameter which is the ID of the component being added.
 * This id is used for data binding.
 *
 * @author Rich O'Connell
 */
public class DataBoundPanel extends JPanel {

	private static final long serialVersionUID = 1024113142112025637L;

	private Map<String, Component> comps = new HashMap<String, Component>();


	/**
     * Creates a new <code>DataBoundPanel</code> with a double buffer and a flow layout.
	 */
	public DataBoundPanel() {
		super();
	}

	/**
     * Creates a new <code>DataBoundPanel</code> with <code>FlowLayout</code>
     * and the specified buffering strategy.
     * If <code>isDoubleBuffered</code> is true, the <code>JPanel</code>
     * will use a double buffer.
     *
     * @param isDoubleBuffered  a boolean, true for double-buffering, which
     *        uses additional memory space to achieve fast, flicker-free
     *        updates
	 */
	public DataBoundPanel(boolean isDoubleBuffered) {
		super(isDoubleBuffered);
	}

	/**
 	 * Creates a new DataBoundPanel with the specified layout manager and buffering
     * strategy.
     * @param layout  the LayoutManager to use
     * @param isDoubleBuffered  a boolean, true for double-buffering, which
     *        uses additional memory space to achieve fast, flicker-free
     *        updates
	 */
	public DataBoundPanel(LayoutManager layout, boolean isDoubleBuffered) {
		super(layout, isDoubleBuffered);
	}

    /**
     * Create a new buffered JPanel with the specified layout manager
     * @param layout  the LayoutManager to use
     */
	public DataBoundPanel(LayoutManager layout) {
		super(layout);
	}

	/**
	 * Tie the given component to it's identifier.
	 * @param identifier the id
	 * @param component the component to be id'd
	 */
	protected void id(final String identifier, final Component component) {
		comps.put(identifier, component);
	}

	/**
	 * Get the components that have bound ID's in this panel.
	 * @return
	 */
	protected Map<String, Component> getComps() {
		return comps;
	}

	/**
	 * Add the component and id it.
	 * @see JComponent#add(Component, int)
	 * @param identifier
	 * @param comp
	 * @param index
	 * @return
	 */
	public Component addBound(final String identifier, final Component comp, final int index) {
		id(identifier, comp);
		return super.add(comp, index);
	}

	/**
	 * Add the component and id it.
	 * @see JComponent#add(Component, Object, int)
	 * @param identifier
	 * @param comp
	 * @param constraints
	 * @param index
	 */
	public void addBound(final String identifier, final Component comp, final Object constraints, final int index) {
		id(identifier, comp);
		super.add(comp, constraints, index);
	}

	/**
	 * Add the component and id it.
	 * @see JComponent#add(Component, Object)
	 * @param identifier
	 * @param comp
	 * @param constraints
	 */
	public void addBound(final String identifier, final Component comp, final Object constraints) {
		id(identifier, comp);
		super.add(comp, constraints);
	}

	/**
	 * Add the component and id it.
	 * @see JComponent#add(Component)
	 * @param identifier
	 * @param comp
	 * @return
	 */
	public Component addBound(final String identifier, final Component comp) {
		id(identifier, comp);
		return super.add(comp);
	}

}
